-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.c
More file actions
147 lines (132 loc) · 3.44 KB
/
Solution.c
File metadata and controls
147 lines (132 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 10
typedef struct Node {
int key;
int value;
struct Node* next;
} Node;
typedef struct HashMap {
Node* table[TABLE_SIZE];
} HashMap;
// Hash function
int hashFunction(int key) {
return key % TABLE_SIZE;
}
// Create a new node
Node* createNode(int key, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
return newNode;
}
// Initialize hash map
void initializeHashMap(HashMap* map) {
for (int i = 0; i < TABLE_SIZE; i++) {
map->table[i] = NULL;
}
}
// Insert into hash map
void insert(HashMap* map, int key, int value) {
int index = hashFunction(key);
Node* newNode = createNode(key, value);
if (map->table[index] == NULL) {
map->table[index] = newNode;
} else {
Node* temp = map->table[index];
while (temp->next != NULL) {
if (temp->key == key) {
temp->value = value; // Update value if key already exists
free(newNode);
return;
}
temp = temp->next;
}
temp->next = newNode;
}
}
// Get value by key
int get(HashMap* map, int key) {
int index = hashFunction(key);
Node* temp = map->table[index];
while (temp != NULL) {
if (temp->key == key) {
return temp->value;
}
temp = temp->next;
}
return -1; // Key not found
}
// Delete a key-value pair
void deleteKey(HashMap* map, int key) {
int index = hashFunction(key);
Node* temp = map->table[index];
Node* prev = NULL;
while (temp != NULL) {
if (temp->key == key) {
if (prev == NULL) {
map->table[index] = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
return;
}
prev = temp;
temp = temp->next;
}
printf("Key not found.\n");
}
// Print hash map
void printHashMap(HashMap* map) {
for (int i = 0; i < TABLE_SIZE; i++) {
printf("Index %d: ", i);
Node* temp = map->table[i];
while (temp != NULL) {
printf("(%d -> %d) ", temp->key, temp->value);
temp = temp->next;
}
printf("\n");
}
}
int main() {
HashMap map;
initializeHashMap(&map);
int choice, key, value;
while (1) {
printf("\n1. Insert\n2. Get\n3. Delete\n4. Print\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter key and value: ");
scanf("%d %d", &key, &value);
insert(&map, key, value);
break;
case 2:
printf("Enter key: ");
scanf("%d", &key);
value = get(&map, key);
if (value == -1)
printf("Key not found.\n");
else
printf("Value: %d\n", value);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
deleteKey(&map, key);
break;
case 4:
printHashMap(&map);
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}